﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpAttributeTeach
{
    /// <summary>
    /// 【1】自定义特性的使用说明
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property,AllowMultiple =true,Inherited =true)]
    public class MyCustomAttribute : Attribute
    {
        /// <summary>
        /// 【2】特性类中，可以直接添加属性
        /// </summary>
        public int CourseId { get; set; }

        public string Comment { get; set; }

        /// <summary>
        /// 【3】特性类中，可以增加构造方法，和普通类使用一样
        /// </summary>
        public MyCustomAttribute() { }

        public MyCustomAttribute(int courseId)
        {
            this.CourseId = courseId;
        }

        public MyCustomAttribute(string comment)
        {
            this.Comment = comment;
        }

        public MyCustomAttribute(int courseId,string comment)
        {
            this.CourseId = courseId;
            this.Comment = comment;
        }

        /// <summary>
        /// 【4】特性类中，可以添加方法
        /// </summary>
        /// <returns></returns>
        public string GetMyCustomInfo()
        {
            return $"课程编号：{CourseId}  课程说明：{Comment}";
        }
    }

    /// <summary>
    /// 特性类中，可以直接使用继承
    /// </summary>
    public class YourCustomAttribute : MyCustomAttribute
    {
        public string Name { get; set; }
    }

    /// <summary>
    /// 常用1：增加标识
    /// </summary>
    public class PrimaryKeyAttribute : Attribute
    {
        public bool IsPKey { get; set; } = true;
    }

    /// <summary>
    /// 常用2：注入信息
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
    public class TableNameAttribute : Attribute
    {
        public string TableName { get; set; }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="tName"></param>
        public TableNameAttribute(string tName)
        {
            this.TableName = tName;
        }
    }

    /// <summary>
    /// 特性类的具体使用
    /// </summary>
    [MyCustom]
    [MyCustom(Comment = "视觉和运动控制系列")]
    [MyCustom(CourseId = 3000)]
    [TableName("MyTeachers")]
    public class Teacher
    {
        //【1】增加标识
        [PrimaryKey]
        public int TeacherId { get; set; }

        //【2】注入信息
        [MyCustom(CourseId = 2000,Comment = "主要老师上位机开发课程")]
        public string TeacherName { get; set; }

        public string Course {  get; set; }
    }

    /// <summary>
    /// 自定义特性信息的读取
    /// </summary>
    public class Practise
    {
        public void Test()
        {
            Teacher teacher = new Teacher();
            DisPlayClassCustomAttributes(teacher);
        }

        public void DisPlayClassCustomAttributes<T>(T model) where T : class
        {
            Type type = typeof(T);

            //获取类上面的自定义特性
            if(type.IsDefined(typeof(MyCustomAttribute),true))
            {
                var myCustomAttributes = type.GetCustomAttributes(typeof(MyCustomAttribute),true);

                foreach(var item in myCustomAttributes)
                {
                    MyCustomAttribute attribute = item as MyCustomAttribute;
                    if(attribute != null)
                    {
                        Console.WriteLine($"课程编号:{attribute.CourseId} 说明信息:{attribute.Comment}");
                    }
                }
            }
        }
    }
}
